/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import csse3004.GD.DA.LoginDA;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 *
 * @author David Bullock 42062222
 */
public class createUserValidator implements Validator {

    LoginService service = new LoginDA();
    protected final Log log = LogFactory.getLog(getClass());

    public boolean supports(Class type) {
        return createUser.class.equals(type);
    }

    public void validate(Object o, Errors errors) {
        createUser user = (createUser) o;
        String name = user.getUserName();
        String type = user.getType();
        String expiry = user.getExpire();
        String email = user.getEmail();
        if (email==null|| email.isEmpty()){
            errors.rejectValue("email", "error.not-specified", null,
                    "Please enter an email");
        }
        if (name==null || name.isEmpty()){
            errors.rejectValue("userName", "error.not-specified", null,
                    "Please enter a user name");
        }
        if (type.equals("0")) {
            errors.rejectValue("type", "error.not-specified", null,
                    "Please select a user type");
        } else if (type.equals("Admin")) {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

            // declare and initialize testDate variable, this is what will hold
            // our converted string

            Date testDate = null;

            // we will now try to parse the string into date form
            try {
                testDate = sdf.parse(expiry);
                // dateformat.parse will accept any date as long as it's in the format
                // you defined, it simply rolls dates over, for example, december 32
                // becomes jan 1 and december 0 becomes november 30
                // This statement will make sure that once the string
                // has been checked for proper formatting that the date is still the
                // date that was entered, if it's not, we assume that the date is invalid

                if (!sdf.format(testDate).equals(expiry)) {
                    errors.rejectValue("expire", "error.not-specified", null,
                            "the date you provided is in an invalid date.");
                }
                Date date = new Date();
                if (testDate.before(date)){
                    errors.rejectValue("expire", "error.not-specified", null,
                            "The date you provided is before the current date.");
                }
            } // if the format of the string provided doesn't match the format we
            // declared in SimpleDateFormat() we will get an exception
            catch (ParseException e) {
                errors.rejectValue("expire", "error.not-specified", null,
                        "the date you provided is in an invalid date"
                        + " format.");
            }


        }



    }
}
